import {Component, EventEmitter, Input, OnInit, Output, ViewChild} from '@angular/core';
import {FormBuilder, FormControl, FormGroup, ValidationErrors, Validators} from '@angular/forms';
import {BaseService} from '../../../shared/service/base.service';
import {ApiUrl} from '../../../config/api-url';

@Component({
  selector: 'app-forget-password',
  templateUrl: './forget-password.component.html',
  styleUrls: ['./forget-password.component.less']
})
export class ForgetPasswordComponent implements OnInit {
  @Input()
  forgetPasswordVisible = false;
  @Output()
  settingForgetPassword: EventEmitter<any> = new EventEmitter<any>();

  isSpinning: any;

  title = '忘记密码';

  validateForm!: FormGroup;

  smsCodeText: any = '获取验证码';
  timer: any;
  time: number;
  smsCodeStatus = false;
  isLoadingOne: boolean;

  constructor(private fb: FormBuilder, private baseService: BaseService) {
  }

  initForm() {
    this.validateForm = this.fb.group({
      mobile: [null, [Validators.required]],
      verification: [null, [Validators.required]],
      newPassword: [null, [Validators.required]],
      confirm: ['', [this.confirmValidator]],
    });
  }

  //验证密码一至性
  confirmValidator = (control: FormControl): { [s: string]: boolean } => {
    if (!control.value) {
      return {error: true, required: true};
    } else if (control.value !== this.validateForm.controls.newPassword.value) {
      return {confirm: true, error: true};
    }
    return {};
  };

  ngOnInit(): void {
    this.initForm();
  }

  closeModal() {
    this.forgetPasswordVisible = false;
    this.settingForgetPassword.emit(this.forgetPasswordVisible);
    this.validateForm.reset({});
  }

  handleCancel(): void {
    this.closeModal();
  }

  submit() {
    this.isSpinning = true;
    if (!this.baseService.zorro.validateFormValidate(this.validateForm)) {
      this.isSpinning = false;
      return;
    }
    this.baseService.post2(ApiUrl.UserUrl + '/forgetPassword', this.validateForm.value).subscribe(data => {
      this.isSpinning = false;
      if (data.status == 0) {
        this.baseService.layer.messageSuccess('重置密码成功！');
        this.closeModal();
      } else {
        this.baseService.layer.messageError('重置密码失败！' + data.msg);
      }
    });
  }

  getCode() {
    this.isSpinning = true;
    let reg = /^0?(13|14|15|17|18)[0-9]{9}$/;
    if (!reg.test(this.validateForm.get('mobile').value)) {
      this.isSpinning = false;
      this.baseService.layer.messageError('请填写正确的手机号码!');
      return;
    }
    if (this.smsCodeStatus) {
      this.isSpinning = false;
      return;
    }
    this.isLoadingOne = true;
    this.baseService.post2(ApiUrl.UserUrl + '/getCode', this.validateForm.value).subscribe(data => {
      this.isSpinning = false;
      if (data.status == 0) {
        clearInterval(this.timer);
        this.time = 59;
        this.smsCodeStatus = true;
        this.timer = setInterval(() => {
          if (this.time > 0) {
            this.time--;
            this.smsCodeText = this.time + '秒';
          } else {
            this.smsCodeText = '重新发送';
            this.smsCodeStatus = false;
            clearInterval(this.timer); //清除计时器
          }
          this.isLoadingOne = false;
        }, 1000);
        this.baseService.layer.messageSuccess(data.data);
      } else {
        this.baseService.layer.messageError('获取验证码失败！' + data.msg);
      }
    });
  }

  validateConfirmPassword(): void {
    setTimeout(() => this.validateForm.controls.confirm.updateValueAndValidity());
  }
}
